package com.xp.record25.bio.netty.basic;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class ServerIP {
    public static void main(String[] args) {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface iface = interfaces.nextElement();
                // 排除虚拟接口、Docker 接口、非活动接口
                if (iface.isLoopback() || iface.isVirtual() || !iface.isUp() || iface.getDisplayName().contains("docker")) {
                    continue;
                }

                // 遍历该接口的 IP 地址
                Enumeration<InetAddress> addresses = iface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    // 过滤 IPv4 地址，排除本地回环地址
                    if (!addr.isLoopbackAddress() && addr.getHostAddress().indexOf(':') == -1) {
                        System.out.println("Valid IP: " + addr.getHostAddress());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}